<?php

namespace App\Http\Controllers\api\v2;

use Illuminate\Http\Request;
use App\Http\Controllers\Api\Controller;
use App\Service\ElkSearch;
use App\Http\Requests\api\v2\ElaRequest;

class ElasticSearchController extends Controller
{
    /*获取文档*/
    public function show(Request $request)
    {
        $search = new ElkSearch();
        $search->getData($request->index, $request->type, $request->id);
    }

    /*索引文档*/
    public function store(Request $request)
    {

    }

    /*搜索文档*/
    public function index(Request $request)
    {
        $this->validate($request, [
            'index' => 'required|min:1|max:10',
            'type' => ['required','min:1','max:10'],
            'field' => 'required',
            'keyword' => ['required','regex:/^[a-zA-Z]+/']
        ],['regex'=>':attribute 参数不规范'],['keyword'=>'关键字']);
        $search = new ElkSearch();
        $page = $request->page ?? $search->getPageNo();
        $limit = $request->limit ?? $search->getLimit();
        $search->setPageNo($page);
        $search->setLimit($limit);
        $data =  $search->search($request->index, $request->type, $request->field, $request->keyword);
        $result = [];
        $result['total'] = $data['hits']['total'];
        $result['list'] = $data['hits']['hits'];
        $response = $this->printData(200, "", $result);
        return $response;
    }

    /*删除文档*/
    public function destroy()
    {

    }
}
